<?php
/**
 * Created by PhpStorm.
 * User: luohuaming
 * Date: 2016/8/4
 * Time: 19:31
 */

namespace Admin\Model;


use Think\Model;

class RoleModel extends Model
{
    /**
     * @param $map 查询条件
     * @param $pagenumber 页号
     * @param $pagesize 页面大小
     * @return mixed
     */
    public function getByConditionPage($map, $pagenumber, $pagesize)
    {
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $this->where($map)->limit($limit)->select();
        for ($i = 0; $i < count($list); $i++) {
            $where['t_role.id'] = $list[$i]['id'];
            $privilege = $this->join('LEFT JOIN  t_role_privilege rp on t_role.id = rp.role_id')
                ->join('LEFT JOIN t_privilege p ON rp.pri_id = p.id')
                ->where($where)->order('id desc')->field('p.*')->select();
            $list[$i]['privilege']=$privilege;
        }
        return $list;
    }

    /**
     * 获取某个角色的所有权限
     * @param $roleId 角色Id
     * @return 返回某个角色的权限列表
     */
    public function getPrivilegeListByRole($roleId){
        $where['t_role.id'] = ':role_id';
        $list = $this->join('LEFT JOIN  t_role_privilege rp on t_role.id = rp.role_id')
            ->join('LEFT JOIN t_privilege p ON rp.pri_id = p.id')
            ->where($where)->bind(':role_id',$roleId)->field('p.*')->select();
        return $list;
    }

    /**
     * 通过管理员id获取他所属的所有角色
     * @param $adminId 管理员ID
     * @return 返回管理员所属的角色
     */
    public function getListByAdmin($adminId){
        $where['a.id'] = ':admin_id';
        $list = $this->join('LEFT JOIN  t_admin_role ar on t_role.id = ar.role_id')
            ->join('LEFT JOIN t_admin a ON ar.admin_id= a.id')
            ->where($where)->bind($where['a.id'],$adminId)->field('t_role.*')->select();
        return $list;
    }
}